<template>
	<view class="page">
		<fu-custom :isBack="true" :isBottom="true" bgColor="bg-white">
			<view slot="content">{{i18n["设置"]}}</view>
		</fu-custom>
		<view class="fu-fs28 padding-bottom-140">
			<view class="padding-lr bg-white margin-bottom" @tap="handleJump"
				data-url="/pages/index/setting/account-safe/index">
				<view class="flex align-center justify-between padding-tb ">
					<text class="fu-fs32 text-333">{{i18n["账号与安全"]}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 语言切换 start -->
			<!-- <view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/language/index">
      	<view class="flex align-center justify-between padding-tb solid-bottom">
      		<text class="fu-fs32 text-333">{{i18n['语言']}}</text>
      		<text class="cuIcon-right"></text>
      	</view>
      </view> -->
			<!-- 语言切换 end -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/personalized/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['个性化推荐设置']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view class="padding-lr bg-white" @tap="handleJump"
				data-url="/pages/index/setting/notification-settings/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['通知设置']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view class="padding-lr bg-white margin-bottom" @tap="handleJump"
				data-url="/pages/index/setting/authen-manager/index">
				<view class="flex align-center justify-between padding-tb ">
					<text class="fu-fs32 text-333">{{i18n['权限管理']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 用户协议 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/user/login/user-agreement/index?id=1">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['用户协议']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 用户协议 end -->
			<!-- 隐私协议 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/user/login/user-agreement/index?id=2">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['隐私政策']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 隐私协议 end -->
			<!-- 意见反馈 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/help/help/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['帮助与反馈']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 意见反馈 end -->
			<!-- 关于我们 start -->
			<view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/setting/about/index?id=6">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['关于我们']}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- 关于我们 end -->
			<!-- 联系我们 start TODO 原型上没有联系我们-->
			<!-- <view class="padding-lr bg-white" @tap="handleJump" data-url="/pages/index/contactUs/index">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">联系我们</text>
					<text class="cuIcon-right"></text>
				</view>
			</view> -->
			<!-- 联系我们 end -->

			<!-- #ifdef APP-PLUS -->
			<!-- 清除缓存 start -->
			<view @tap="clear" class="margin-top-sm padding-lr bg-white">
				<view class="flex align-center justify-between padding-tb solid-bottom">
					<text class="fu-fs32 text-333">{{i18n['清除缓存']}}</text>
					<text>
						<text>{{fileSizeString}}</text>
						<text class="cuIcon-right"></text>
					</text>
				</view>
			</view>
			<!-- 清除缓存 end -->
			<!-- 系统版本 start -->
			<view @tap="handleTestingVersion" class="padding-lr bg-white padding-right">
				<view class="flex align-center justify-between padding-tb">
					<text class="fu-fs32 text-333">{{i18n['检查更新']}}</text>
					<text>
						<text>v{{version}}</text>
						<text class="cuIcon-right"></text>
					</text>
				</view>
			</view>
			<!-- 系统版本 end -->
			<!-- #endif -->
			<view @tap="cancelAcount" class="padding-lr bg-white  margin-top">
				<view class="flex align-center justify-between padding-tb">
					<text class="fu-fs32 text-333">{{i18n['注销账号']}}</text>
					<view class="">
						<text v-if="status==2||status==0" :class="status==2?'text-red':'text-666'">{{status==2?'平台拒绝':'待审核'}}</text>
						<text class="cuIcon-right"></text>
					</view>
				</view>
			</view>
		</view>
		<!-- 退出登录按钮 end -->
		<view class="btn_cloce bg-white" v-if="isLogin">
			<view class="height-110 line-height-110 text-theme fu-fs32 text-center" @click="logout">{{i18n['退出登录']}}
			</view>
		</view>
		<!-- 退出登录按钮 end -->
		<!-- APP更新组件 start -->
		<app-update ref="update"></app-update>
		<!-- APP更新组件 end -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				version: '', // 版本号
				fileSizeString: '0B', // 缓存大小
				status: '', //注销账号状态
				oldPhone: true, // 是否是老旧设备 true: 不支持指纹或人脸识别 false: 支持指纹或人脸识别
			};
		},
		computed: {
			// 是否登录状态
			isLogin() {
				return this.$store.getters.isLogin;
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
			// 获取App版本号
			plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
				this.version = widgetInfo.version;
			})
			this.formatSize();
			// #endif
			this.getData()
			uni.$on('cancelAccount', data => {
				this.getData()
			})
		},
		onUnload() {
			uni.$off('cancelAccount')
		},
		methods: {
			// 注销账号
			cancelAcount() {
				if (this.status === -1 || this.status === 2) {
					this.$urouter.navigateTo({
						url: '/pages/index/setting/cancel-account/cancel-account/index'
					})
				} else if (this.status === 0) {
					this.$urouter.navigateTo({
						url: '/pages/index/setting/cancel-account/cancel-loading/index'
					})
				}
			},
			// 获取状态
			getData() {
				this.$api.post(global.apiUrls.cancelAccountAgreement).then(res => {
					if (res.data.code == 1) {
						this.status = res.data.data;
					} else {
						this.$message.info(res.data.msg);
					}
				}).catch(() => {})
			},
			// 更新检测
			handleTestingVersion() {
				this.$util.debounce(() => {
					this.$refs.update.checkAppUpdate();
				}, 1000, true)
			},
			// 退出登录
			logout() {
				this.$util.showModal({
					title: this.i18n['提示'],
					content: this.i18n['确定退出当前账号？'],
					confirmColor: this.themeColor,
					confirmText: this.i18n['确定'],
					cancelText: this.i18n['取消'],
					success: res => {
						if (res.confirm) {
							this.$store.commit('logout');
							uni.navigateBack();
							this.$store.dispatch('getCartList');
						}
					}
				});
			},
			// 清除本地缓存
			clear() {
				this.$util.showModal({
					title: this.i18n['提示'],
					content: this.i18n['确定要清除本地缓存？'],
					confirmColor: this.themeColor,
					confirmText: this.i18n['确定'],
					cancelText: this.i18n['取消'],
					success: res => {
						if (res.confirm) {
							this.clearCache();
						}
					}
				})
			},
			// 格式化缓存大小
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = '0B';
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + 'B';
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + 'KB';
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + 'MB';
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + 'GB';
					}
				});
			},
			// 清除本地缓存
			clearCache() {
				let user = uni.getStorageSync('USER_INFO');
				uni.clearStorage();
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, 'listFiles');
					let len = files.length;
					if (len == 0) return that.$message.info(that.i18n['已经很干净了，没有需要清理的内容!']);
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
						plus.io.resolveLocalFileSystemURL(
							filePath,
							function(entry) {
								if (entry.isDirectory) {
									entry.removeRecursively(
										function(entry) {
											//递归删除其下的所有文件及子目录
											uni.setStorageSync('USER_INFO', user);
											that.$message.info(that.i18n['缓存清理完成!']);
											that.formatSize(); // 重新计算缓存
										},
										function(e) {
											console.log(e.message);
										}
									);
								} else {
									entry.remove();

								}
							},
							function(e) {
								console.log('文件路径读取失败');
							}
						);
					}
				} else {
					// ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错
					plus.cache.clear(function() {
						this.$message.info(this.i18n['缓存清理完成!']);
						that.formatSize();
					});
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	// 退出登录按钮 start
	.btn_cloce {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-shadow: 0 0 10rpx #EEEEEE;
	}

	// 退出登录按钮 end
</style>